package com.ls.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;

public class NIOFileChannel {
    public static final File file = new File("d:\\file00.txt");
    public static final File file1 = new File("d:\\file01.txt");
    public static final File file2 = new File("d:\\file02.txt");
    public static final String string1 = "E:\\s\\0.jpg";
    public static final String string2 = "E:\\t\\00.jpg";


    public static void main(String[] args) throws Exception {
//        channelWrite();
//        channelRead();
//        channelReadAndWrite();
//        channelTransferFrom();
        channelServerSocketChanel();


    }


    public static void channelServerSocketChanel() throws Exception {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        InetSocketAddress inetSocketAddress = new InetSocketAddress(7777);

        serverSocketChannel.socket().bind(inetSocketAddress);

        ByteBuffer[] byteBuffers = new ByteBuffer[2];
        byteBuffers[0] = ByteBuffer.allocate(5);
        byteBuffers[1] = ByteBuffer.allocate(3);

        SocketChannel socketChannel = serverSocketChannel.accept();
        int messageLength = 8;//假定从客户端接收 8个字节

        while (true) {

            int byteRead = 0;

            while (byteRead < messageLength) {
                System.out.println("+++++++++++");
                long l = socketChannel.read(byteBuffers);
                System.out.println("-----------");
                byteRead += l;//累计读取数
                System.out.println("byteRead: " + byteRead);
                //流打印
                Arrays.asList(byteBuffers).stream().map((buffer) ->
                        "postion=" + buffer.position() + ", limit= " + buffer.limit()
                ).forEach(System.out::println);
            }

            Arrays.asList(byteBuffers).forEach(buffer -> buffer.flip());

            //将数据显示到客户端
            long byteWrite = 0;
            while (byteWrite < messageLength) {
                long l = socketChannel.write(byteBuffers);
                byteWrite += l;
                System.out.println("byteWrite: " + byteWrite);
            }

            Arrays.asList(byteBuffers).forEach(buffer -> buffer.clear());
            System.out.println(byteRead + "****" + byteWrite);
            System.out.println(messageLength);
        }


    }


    //拷贝 TransferFrom
    public static void channelTransferFrom() {
        try (FileInputStream fileInputStream = new FileInputStream(string1);
             FileOutputStream fileOutputStream = new FileOutputStream(string2);
             FileChannel channel1 = fileInputStream.getChannel();
             FileChannel channel2 = fileOutputStream.getChannel();) {


            channel2.transferFrom(channel1, 0, channel1.size());


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void channelReadAndWrite() {
        try (FileInputStream fileInputStream = new FileInputStream(file1);
             FileOutputStream fileOutputStream = new FileOutputStream(file2);) {


            final FileChannel channel1 = fileInputStream.getChannel();
            final FileChannel channel2 = fileOutputStream.getChannel();

            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

            while (true) {
                byteBuffer.clear();
                int read = channel1.read(byteBuffer);
                if (read == -1) {
                    break;
                }
                byteBuffer.flip();
                channel2.write(byteBuffer);

            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void channelRead() {
        try (FileInputStream fileInputStream = new FileInputStream(file);) {
            final FileChannel channel = fileInputStream.getChannel();
            //创建一个缓冲区 ByteBuffer
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
            channel.read(byteBuffer);
            System.out.println(new String(byteBuffer.array()));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void channelWrite() {
        String s = "hello0";
        //创建一个输出流——》channel
        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {

            final FileChannel channel = fileOutputStream.getChannel();

            //创建一个缓冲区 ByteBuffer
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.put(s.getBytes());
            byteBuffer.flip();//由于写完之后position在最后，所以读的时候需要翻转

            channel.write(byteBuffer);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

